
//
//  TL_GoogleManager.m
//  WXReader
//
//  Created by LL on 2020/9/6.
//  Copyright © 2020 Andrew. All rights reserved.
//

#import "TL_GoogleManager.h"

@interface TL_GoogleManager ()

@property (nonatomic, assign) TL_GoogleState state;

@end

@implementation TL_GoogleManager

SINGLETON_IMPLEMENTATION(TL_GoogleManager)

- (void)tunedUpGoogleWithState:(TL_GoogleState)state {
    _state = state;
    GIDConfiguration *configuration = [[GIDConfiguration alloc] initWithClientID:WX_Google_ID];
    [GIDSignIn sharedInstance].configuration = configuration;
    
    [[GIDSignIn sharedInstance] signInWithPresentingViewController:[TL_ViewHelper getCurrentViewController] completion:^(GIDSignInResult * _Nullable signInResult, NSError * _Nullable error) {
        if (error) {
            [self googleFail:_state == TL_GoogleStateLogin ? WXLocalizedString(@"登录失败") : WXLocalizedString(@"绑定失败")];
        } else {
            [self thirdPartyLogin:signInResult.user.accessToken.tokenString];
            NSLog(@"idtoken : %@",signInResult.user.idToken.tokenString);
        }
    }];
}

/// 第三方登录
- (void)thirdPartyLogin:(NSString *)token {
    if (kObjectIsEmpty(token)) {
        [self googleFail:self.state == TL_GoogleStateLogin ? WXLocalizedString(@"登录失败") : WXLocalizedString(@"绑定失败")];
    } else {
        MK_WEAK_SELF
        NSString *alertText = self.state == TL_GoogleStateLogin ? WXLocalizedString(@"正在登录") : WXLocalizedString(@"正在绑定");
        MBProgressHUD *loginHUD = [kMainWindow showDarkHUDFromText:alertText];
        
//        [TL_AnalyticsManager logEvent:@"login" params:@{@"plateform":@"2"}];

        [APIManager sendPostRequest:_state == TL_GoogleStateLogin ? [APIURLManager Google_Login] : [APIURLManager Google_Bind] parameters:@{@"accessToken":token} model:TL_UserInfoModel.class success:^(BOOL success, TL_UserInfoModel* _Nullable responseData, APIResponse * _Nonnull requestModel) {
            [loginHUD hideAnimated:YES];
            if (success) {
                if (responseData.is_new) {
//                    [TL_AnalyticsManager logEvent:@"registration" params:@{}];
                }
                
                if ([weakSelf.delegate respondsToSelector:@selector(googleLoginSuccess:userInfo:)]) {
                    [weakSelf.delegate googleLoginSuccess:weakSelf userInfo:TL_UserInfoModel.shareInstance];
                }
            } else {
                [self googleFail:requestModel.msg ?: (self.state == TL_GoogleStateLogin ? WXLocalizedString(@"登录失败") : WXLocalizedString(@"绑定失败"))];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [loginHUD hideAnimated:YES];
            [self googleFail:self.state == TL_GoogleStateLogin ? WXLocalizedString(@"登录失败") : WXLocalizedString(@"绑定失败")];
        }];
        
        
        [APIManager sendPostRequest:_state == TL_GoogleStateLogin ? [APIURLManager Google_Login] : [APIURLManager Google_Bind] parameters:@{@"accessToken":token} model:TL_UserInfoModel.class success:^(BOOL success, id  _Nullable responseData, APIResponse * _Nonnull requestModel) {
            
            [loginHUD hideAnimated:YES];
            if (success) {
                if ([weakSelf.delegate respondsToSelector:@selector(googleLoginSuccess:userInfo:)]) {
                    [weakSelf.delegate googleLoginSuccess:weakSelf userInfo:TL_UserInfoModel.shareInstance];
                }
            } else {
                [self googleFail:requestModel.msg ?: (self.state == TL_GoogleStateLogin ? WXLocalizedString(@"登录失败") : WXLocalizedString(@"绑定失败"))];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [loginHUD hideAnimated:YES];
            [self googleFail:self.state == TL_GoogleStateLogin ? WXLocalizedString(@"登录失败") : WXLocalizedString(@"绑定失败")];
        }];
        loginHUD.closeBlock = ^{
            [self googleFail:self.state == TL_GoogleStateLogin ? WXLocalizedString(@"登录失败") : WXLocalizedString(@"绑定失败")];
        };
        
    }
}

- (void)googleFail:(NSString *)error {
    if ([self.delegate respondsToSelector:@selector(googleLoginFail:error:)]) {
        [self.delegate googleLoginFail:self error:error];
    }
}

@end
